package com.gexin.weixin.uitls;

import com.gexin.common.utils.DateUtil;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.CustomServiceApi;
import com.jfinal.weixin.sdk.cache.RedisAccessTokenCache;

import java.util.Date;

/**
 * 微信消息通知
 * @author Administrator
 *
 */
public class WeiXinMessageNoticeApi {
	private static final String notice_new_fans="新增粉丝通知！\n昵称：<a href=\""+ WxConfig.WX_JS_HOST+"/wx/channel/team\">%s</a>\n时间:%s";
	private static final String notice_new_income="新增收入提醒\n订单ID：<a href=\""+ WxConfig.WX_JS_HOST+"/wx/account\">%s</a>\n金额:￥%s\n时间:%s";
	private static final String notice_Add_Money="零钱入账\n订单ID：<a href=\""+ WxConfig.WX_JS_HOST+"/wx/account\">%s</a>\n金额:￥%s\n时间:%s";
	public static void main(String[] args) {
		PropKit.use("gexin_config_debug.txt");
		RedisPlugin bbsRedis = new RedisPlugin(PropKit.get("redis.name"), PropKit.get("redis.ip"), PropKit.getInt("redis.port"), 2000, PropKit.get("redis.pass"), PropKit.getInt("redis.database"));
		bbsRedis.start();
		ApiConfig Api_Config=new ApiConfig();
		Api_Config.setAppId(PropKit.get("AppID"));
		Api_Config.setAppSecret(PropKit.get("AppSecret"));
		Api_Config.setToken(PropKit.get("Token"));
		ApiConfigKit.setThreadLocalAppId(WxConfig.Api_Config.getAppId());
		ApiConfigKit.setAccessTokenCache(new RedisAccessTokenCache());
		WxConfig.Api_Config=Api_Config;
		//sendText("oFcHvsgunqBDnlzFVWe4Wf-HtvPU","bbb");
		WeiXinMessageNoticeApi.notice_New_Fans("oFcHvsgunqBDnlzFVWe4Wf-HtvPU","程序人生");
		WeiXinMessageNoticeApi.notice_New_Income("oFcHvsgunqBDnlzFVWe4Wf-HtvPU","98554120545454515","22.05");
		WeiXinMessageNoticeApi.notice_Add_Money("oFcHvsgunqBDnlzFVWe4Wf-HtvPU","98554120545454515","22.05");
	}
	
	/**
	 * 推送0元购物消息
	 * */
	public static void sendLinYuanGouWu(String openId){
		
	/*	ApiConfigKit.setThreadLocalApiConfig(WxConfig.Api_Config);
		List<Articles>  list =new ArrayList<Articles>();
		Articles  a =new Articles();
		a.setUrl(WxConfig.WX_JS_HOST+"/wx/product/coupon_group");
		a.setTitle("您获得了一次0元购物的机会！");
		a.setDescription("0元即可获得一份免单商品！\n 点击进入免单商品列表，随意挑选>>>>");
		a.setPicurl("https://mmbiz.qlogo.cn/mmbiz_jpg/9x7mle3PkeN3AG4MG1FFjl59lP3I6686BCYUp4YzqVpROCrS6GtibNPNUsbNdVyFLy72F2yH1J6rhUFlVRU0SNQ/0?wx_fmt=jpeg");
		list.add(a);
		CustomServiceApi.sendNews(openId,list);
		//保存数据入库
		CouponRelUser  coupon =new CouponRelUser();
		coupon.setCouponId(1);
		coupon.setType(1);
		coupon.setOpenid(openId);
		coupon.setStartDate(new Date());
		coupon.setEndDate(getAddDay(1));
		coupon.save();*/
	}
	
	/**
	 * 获取当前时间减去多天
	 * @param offset  天数
	 * @return  
	 */
	public static Date getAddDay( int offset)  {
		return  new Date(new Date().getTime()+(offset*24)*60*60*1000);
	}
	public static void notice_Add_Money(String openid,String order_id,String money){
		sendText(openid,String.format(notice_Add_Money, order_id,money, DateUtil.getDate()));
	} 
	public static void notice_New_Income(String openid,String order_id,String money){
		sendText(openid,String.format(notice_new_income, order_id,money, DateUtil.getDate()));
	} 
	public static void notice_New_Fans(String openid,String nickname){
		sendText(openid,String.format(notice_new_fans, nickname, DateUtil.getDate()));
	} 
	/**
	 * 发送消息通知
	 * @param openId	
	 * @param text
	 */
	public static void sendText(String openId, String text ){
		ApiConfigKit.setThreadLocalAppId(WxConfig.Api_Config.getAppId());
		CustomServiceApi.sendText(openId,text);
	}
}
